package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.IProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBCUtils;
import redis.clients.jedis.Jedis;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements IProductDao {
    private void clearProductRedis(){
        Jedis jedis = new Jedis();
        jedis.del("product_id");
        jedis.del("product_name");
        jedis.del("product_photo");
        jedis.del("product_user_type");
        jedis.del("product_type");
        jedis.del("product_price");
//        jedis.del("product_create_time");
//        jedis.del("product_update_time");
//        jedis.del("product_enable");
//        jedis.del("product_note");
    }

    private ProductModel getResult(ResultSet rs) throws SQLException {
        ProductModel product = new ProductModel();
        product.setId(rs.getInt("id"));
        product.setName(rs.getString("name"));
        product.setPhoto(rs.getString("photo"));
        product.setUserType(new UserTypeDaoImpl().getUserType(rs.getInt("user_type")));
        product.setProductType(new ProductTypeDaoImpl().getProductType(rs.getInt("product_type")));
        product.setPrice(rs.getBigDecimal("price"));
        product.setCreateTime(rs.getString("create_time"));
        product.setUpdateTime(rs.getString("update_time"));
        product.setEnabled(rs.getInt("enable"));
        product.setNote(rs.getString("note"));
        return product;
    }

    private ProductModel getProductResult(String sql) {
        ResultSet rs = JDBCUtils.getResultSet(sql);
        ProductModel product = null;
        try {
            while (rs.next()) {
                product = getResult(rs);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return product;
    }

    private List<ProductModel> getProductListResult(String sql) {
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (rs.next()) {
                ProductModel product = getResult(rs);
                list.add(product);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<ProductModel> getProductList() {
        String sql = "select * from product";
        return getProductListResult(sql);
    }

    @Override
    public ProductModel getProduct(int id) {
        String sql = "select * from product where id = " + id;
        return getProductResult(sql);
    }

    @Override
    public int addProduct(ProductModel productModel) {
        String sql = "insert into product(name,photo,user_type,product_type,price,create_time)" +
                " values('" + productModel.getName()
                + "','" + productModel.getPhoto()
                + "'," + productModel.getUserType().getId()
                + "," + productModel.getProductType().getId()
                + "," + productModel.getPrice()
                + ",'" + productModel.getCreateTime() + "')";
        clearProductRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteProduct(int id) {
        String sql = "delete from product where id=" + id;
        clearProductRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int updateProduct(ProductModel productModel) {
        String sql = "update product set" +
                " name='" + productModel.getName()
                + "',photo='" + productModel.getPhoto()
                + "',user_type=" + productModel.getUserType().getId()
                + ",product_type=" + productModel.getProductType().getId()
                + ",price=" + productModel.getPrice()
                + ",update_time='" + productModel.getUpdateTime()
                + "' where id=" + productModel.getId();
        clearProductRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<ProductModel> searchProduct(String sql) {
        return getProductListResult(sql);
    }

    @Override
    public List<ProductModel> getProductList(int page, int size) {
        String sql = "select * from product limit " + (page - 1) * size + "," + size;
        return getProductListResult(sql);
    }

    @Override
    public List<ProductModel> getProductList(int page, int size, String sort) {
        String sql = "select * from product order by " + sort + " limit " + (page - 1) * size + "," + size;
        return getProductListResult(sql);
    }

    @Override
    public long getProductCount() {
        String sql = "select count(*) from product";
        return JDBCUtils.getCount(sql);
    }

    @Override
    public int updateEnable(int id, int enable) {
        String sql = "update product set enable=" + enable + " where id=" + id;
        clearProductRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<ProductModel> getProductListByRedis() {
        Jedis jedis = new Jedis();
        List<ProductModel> productList = new ArrayList<>();
        List<String> productId = jedis.lrange("product_id",0,-1);
        for (int i=0;i<productId.size();i++){
            ProductModel product = new ProductModel();
            product.setId(Integer.valueOf(productId.get(i)));
            product.setName(jedis.lindex("product_name",i));
            product.setPhoto(jedis.lindex("product_photo",i));
            product.setUserType(new UserTypeDaoImpl().getUserType(Integer.parseInt(jedis.lindex("product_user_type",i))));
            product.setProductType(new ProductTypeDaoImpl().getProductType(Integer.parseInt(jedis.lindex("product_type",i))));
            product.setPrice(new BigDecimal(jedis.lindex("product_price",i)));
//            product.setCreateTime(jedis.lindex("product_create_time",i));
//            product.setUpdateTime(jedis.lindex("product_update_time",i));
//            product.setEnabled(Integer.valueOf(jedis.lindex("product_enable",i)));
//            product.setNote(jedis.lindex("product_note",i));
            productList.add(product);
        }
        return productList;
    }

    @Override
    public void setProductListInRedis(List<ProductModel> list) {
        Jedis jedis = new Jedis();
        for (ProductModel product:list){
            jedis.rpush("product_id", String.valueOf(product.getId()));
            jedis.rpush("product_name",product.getName());
            jedis.rpush("product_photo",product.getPhoto());
            jedis.rpush("product_user_type", String.valueOf(product.getUserType().getId()));
            jedis.rpush("product_type", String.valueOf(product.getProductType().getId()));
            jedis.rpush("product_price", String.valueOf(product.getPrice()));
//            jedis.rpush("product_create_time",product.getCreateTime());
//            jedis.rpush("product_update_time",product.getUpdateTime());
//            jedis.rpush("product_enable", String.valueOf(product.getEnabled()));
//            jedis.rpush("product_note",product.getNote());
        }
    }
}
